﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Collections;
using WXYW.Model;

namespace WXYW
{
    public partial class ListFormConfirmQuatation : System.Web.UI.Page
    {
        public int RecordsNumber { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindApplicationForm();
            }
        }

        private void BindApplicationForm()
        {
            ApplicationService service = new ApplicationService();
            List<ConfirmQuatationModel> forms = service.GetApplicationFormsNeedConfirmQuatation(keyWordTextBox.Text);
            myApplicationGridView.DataSource = forms;
            myApplicationGridView.DataBind();
            RecordsNumber = forms.Count;
        }

        protected void viewLinkButton_click(object sender, EventArgs e)
        {
            string applicationID = ((LinkButton)sender).CommandArgument;
            Response.Redirect(string.Format("ConfirmQuatationForm.aspx?{0}={1}", Constants.QUERYSTR_APPLICATION_ID, applicationID));
        }

        protected void myApplicationGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= myApplicationGridView.PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedValue = (myApplicationGridView.PageIndex + 1).ToString();
            }
        }

        protected IEnumerable GridView_MustAddARow(IEnumerable data)
        {
            List<ConfirmQuatationModel> dds = new List<ConfirmQuatationModel>();
            dds.Add(new ConfirmQuatationModel());
            return dds;
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            myApplicationGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            BindApplicationForm();
        }

        protected void myApplicationGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            ApplicationService service = new ApplicationService();
            List<ConfirmQuatationModel> formlist = service.GetApplicationFormsNeedConfirmQuatation(keyWordTextBox.Text);

            RecordsNumber = formlist.Count;

            string sortDirection = "asc";
            if (Session["OrderDirection"] != null)
            {
                sortDirection = Session["OrderDirection"].ToString();
                if (sortDirection.Equals("asc"))
                {
                    Session["OrderDirection"] = "desc";
                }
                else
                {
                    Session["OrderDirection"] = "asc";
                }
            }
            else
            {
                Session["OrderDirection"] = "asc";
            }
            
            myApplicationGridView.DataSource = formlist.AsQueryable().OrderBy(string.Format("{0} {1}", e.SortExpression, sortDirection)).ToList();
            myApplicationGridView.DataBind();
        }

        protected void myApplicationGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < myApplicationGridView.PageCount)
            {
                myApplicationGridView.PageIndex = e.NewPageIndex;
                BindApplicationForm();
            }
        }

        protected void myApplicationGridView_DataBound(object sender, EventArgs e)
        {
            if (myApplicationGridView.BottomPagerRow != null)
            {
                myApplicationGridView.BottomPagerRow.Visible = true;
            }
        }

        protected void searchLinkButton_Click(object sender, EventArgs e)
        {
            BindApplicationForm();
        }

        protected void resetLinkButton_Click(object sender, EventArgs e)
        {
            keyWordTextBox.Text = "";
            BindApplicationForm();
        }
    }
}
